package com.hut.bms.controller;

import com.hut.bms.Constant.Constants;
import com.hut.bms.model.Result;
import com.hut.bms.model.UserInfo;
import com.hut.bms.service.UserLoginService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/BMS")
@RestController
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;
    @RequestMapping("/login")
    public Result login(String userName, String password, HttpSession session ) {
        //如果账号或者密码为空，则返回账号或密码为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return Result.fail("账号或密码为空");
        }
        //从数据库中获取用户信息
        UserInfo userInfo = userLoginService.getByUserName(userName);
        //判断用户是否存在
        if(userInfo == null) {
            return Result.fail("用户不存在");
        }
        //判断密码是否正确
        if(!userInfo.getPassword().equals(password)) {
            return Result.fail("密码错误");
        }
        //将整个用户在数据库中的信息存储在session中
        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
        return Result.success("");
    }
}
